<template>
	<el-dropdown trigger="click" @command="changeLanguage">
		<div>
			<svg-icon class-name="international-icon" icon-class="language" />
		</div>
		<el-dropdown-menu slot="dropdown">
			<template v-for="(item, index) in cutList">
				<el-dropdown-item :command="item" :disabled="languageId === item.id" :key="index">{{ item.name
				}}</el-dropdown-item>
			</template>
		</el-dropdown-menu>
	</el-dropdown>
</template>
  
<script>
import { getSystemlanguagesCache } from '@/api/languages'
export default {
	name: 'Lang',
	computed: {
		languageId() {
			// 固定语种ID  1:中文  2:英文 3:俄文 4:乌语
			return this.$store.getters.languageId
		},
		language() {
			// 固定语种ID  1:zh-CN  2:en-US 3:ru 4:
			return this.$store.getters.language
		},
	},
	data() {
		return {
			cutList: [],//语种
			languageList: []
		}
	},
	created() {
		this.getSystemlanguagesCacheData() //语种
		this.$store.dispatch('app/setLanguage', this.language) //语言默认
		this.$i18n.locale = this.language
	},
	watch: {

	},
	methods: {
		// 语种
		getSystemlanguagesCacheData() {
			getSystemlanguagesCache({})
				.then(res => {
					if (res.code === 200) {
						// 处理浏览器刷新 保存之前语种数据
						if (res.data.cutList.length > 0) {
							res.data.cutList.filter(i => {
								if (i.code === this.language) {
									this.$store.dispatch('app/setLanguageId', Number(i.id))
								}
							})
						}
						this.cutList = res.data.cutList
						this.languageList = res.data.languageList //此处定义 切换语种功能点处理数控处理
						// 当前语种对应数据处理
						var arr = []
						if (this.languageId && res.data.languageList) {
							res.data.languageList.filter(i => {
								if (this.languageId === i.langId) {
									arr.push(i)
								}
							})
						}
						localStorage.setItem('languageList', JSON.stringify(arr))////解决浏览器刷新当前页面拿不到 store数据
						this.$store.dispatch('app/setLanguageList', arr)
					}
				})
				.catch(err => {
				})
		},
		changeLanguage(tar) {
			this.$i18n.locale = tar.code // 设置给本地的i18n插件
			// 处理 set page title 页面路由title
			var view = this.$router.currentRoute
			this.$store.dispatch('tagsView/delCachedView', view).then(() => {
				const { fullPath } = view;
				this.$nextTick(() => {
					this.$router.replace({
						path: '/redirect' + fullPath
					});
				});
			});
			this.$store.dispatch('app/setLanguage', tar.code)
			this.$store.dispatch('app/setLanguageId', Number(tar.id))
			var arr = []
			if (this.languageId && this.languageList) {
				this.languageList.filter(i => {
					if (this.languageId === i.langId) {
						arr.push(i)
					}
				})
			}
			this.$emit('onSwitch') ///语种切换成功处理搜索功能点数据重新渲染
			this.$store.dispatch('app/setLanguageList', arr)
			localStorage.setItem('languageList', JSON.stringify(arr))////解决浏览器刷新当前页面拿不到 store数据
			// this.$message.success(`${this.$t('placeholderTip.switchLanguage')}`)
		}
	}
}
</script>
